<%@page import="javax.jms.Connection"%>
<%@page import="javax.jms.TextMessage"%>
<%@page import="javax.jms.MessageProducer"%>
<%@page import="javax.jms.Session"%>
<%@page import="javax.jms.Topic"%>
<%@page import="javax.jms.ConnectionFactory"%>
<%!
    ConnectionFactory connectionFactory;
    Topic topic;
    Connection connection = null;
    Session jmsSession = null;
    MessageProducer messageProducer = null;
    TextMessage message = null;
    public void jspInit(){
        try{
            InitialContext ic = new InitialContext();
            connectionFactory = (ConnectionFactory)ic.lookup("jms/TopicConnectionFactory");
            topic = (Topic) ic.lookup("jms/Topic");
            
            connection = connectionFactory.createConnection();
            jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            messageProducer = jmsSession.createProducer(topic);
        } catch (Exception ex){
            System.out.println("Erreur Message Bean : " + ex.getMessage());
        }
    }
%>
<%@page import="club.jeux.persistence.Joueur"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="club.jeux.session.ServeurLocal"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="template/header.jsp" %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Deconnexion</title>
    </head>
    <body>
    <center>
        <p class="titre">Deconnexion</p>
        <%
         Joueur j = (Joueur) session.getAttribute("joueur");
        if(j == null){
            RequestDispatcher rd = request.getRequestDispatcher("connexion.jsp");
            rd.forward(request, response);
        }else{
             ServeurLocal salle=null;
            try {
            InitialContext ic = new InitialContext();
            Object o = ic.lookup("java:comp/env/ServeurLocal");
            salle = (ServeurLocal) o;
            salle.deconnexion(j);
            session.setAttribute("joueur", null);
            session.setAttribute("partie", null);
            session.setAttribute("FTDisplay", null);
            message = jmsSession.createTextMessage();
            message.setStringProperty("Joueur", j.getLogin());
            message.setText("Un joueur vient de se déconnecter : " + j.getLogin());
            messageProducer.send(message);
            
            response.sendRedirect("index.jsp");

                  }catch(Exception e){
            e.printStackTrace();
            out.println("problème : " + e.toString()); 
        }
                         }
%>
    </center>
<%@include file="template/footer.jsp" %>